﻿var IstoricCererePage = function(pageContainer) {

    IstoricCererePage.superclass.constructor.call(this, pageContainer);

    this.CanGoBack = false;

    var self = this;
    var istoric = new Array();
    var isLoading = false;

    this.CCD_ID = 0;

    var Elements = {
        istoricList: pageContainer.find(".istoricList"),
        loader: pageContainer.find(".loadMore"),
        pullToRefresh: null
    };

    var Templates = {
        istoricTemplate: pageContainer.find(".istoricTemplate"),
        istoricTemplateCompiled: null
    };

    this.getPullDownData = function() {
        self.getPullUpData();
    };

    this.getPullUpData = function(callback) {
        if (isLoading)
            return;

        Elements.istoricList.empty();
        Elements.loader.show();

        isLoading = true;
        app.TMMService.Cereri.GetAudit(self.CCD_ID, true, function(response) {
            isLoading = false;
            Elements.loader.hide();
            if (response.Exception)
                return app.MessagesFactory.showToastWarning(translate('General_Eroare'), translate('General_EroareServer'));

            addToList(response.FinalResult, false);
            $(document).trigger('hidePullDown');
        });
    };

    var addToList = function(data, addToStart) {
        if (!data || !data.length)
            return;

        if (!Templates.istoricTemplateCompiled) {
            var source = Templates.istoricTemplate.html();
            Templates.istoricTemplateCompiled = Handlebars.compile(source);
        }

        var html = "";

        for (var i = 0; i < data.length; i++) {
            var n = data[i];
            n.POZA = Utils.getEmployeePhoto(n.ArePoza ? (n.PRS_ID || n.USR_LOGIN) : 0);
            if (n.Operatie == 0)
                n.Actiune = translate('CereriConcediu_Initiere');
            else if (n.Operatie == 1)
                n.Actiune = translate('CereriConcediu_Aprobare');
            else if (n.Operatie == 2)
                n.Actiune = translate('CereriConcediu_Refuzare');
            else if (n.Operatie == 3)
                n.Actiune = translate('CereriConcediu_Modificare');
            else if (n.Operatie == 4)
                n.Actiune = translate('CereriConcediu_Reinitiere');

            n.Data = Globalize.format(n.DataModificare, "f").toString();
            if (n.Descriere && n.Descriere.length)
                n.Descriere = '"' + n.Descriere + '"';
            else n.Descriere = "";
            html += Templates.istoricTemplateCompiled(n);
        }

        Elements.istoricList.html(html);
    };
    self.initialize = override(self.initialize, function() {
        self.Title = translate('Module_Title_IstoricCerere');
        app.translatePage(app.Pages.IstoricCerere);

        pageContainer.bind('onBeforePageShow', function() {
            app.Phone.setBackbtnVisible(true);
        });
    });

    this.setModuleAction = override(self.setModuleAction, function() {
        if (app.Phone)
            app.Phone.setModuleAction(ModuleActions.None);
    });

    self.initialize();
};
extend(IstoricCererePage, TMMobilePage);